/*
 * Copyright 2010-2011 CGT&DC ISA RAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package websolve.workflow.wfms;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import websolve.workflow.wfms.security.AuthzContext;
import websolve.workflow.wfms.security.AuthzManager;

public class WorkflowResource {

    private String workflowId;
    private ServletContext ctx;
    private SecurityContext securityCtx;

    public WorkflowResource(String name, ServletContext ctx, SecurityContext securityCtx) {
        this.workflowId = name;
        this.ctx = ctx;
        this.securityCtx = securityCtx;
    }

    @GET
    @Produces("application/json")
    public JSONObject getWorkflowJSON() throws JSONException, IOException {
        AuthzManager.getInstance(ctx).authorize(
                new AuthzContext(securityCtx, AuthzContext.WORKFLOW_RESOURCE, workflowId, HttpMethod.GET));
        log.fine("GET " + workflowId + " (JSON) - get workflow");
        System.out.println("Get workflow description request received!");
        JSONObject resp = WorkflowManager.getInstance(ctx).getWorkflow(workflowId).getDescription();
        log.finer("GET " + workflowId + " (JSON) response: " + resp);
        return resp;
    }

    // Browser compatibility DELETE
    @POST
    public Response deleteWorkflow(@QueryParam("method") String method) {
        AuthzManager.getInstance(ctx).authorize(
                new AuthzContext(securityCtx, AuthzContext.WORKFLOW_RESOURCE, workflowId, HttpMethod.DELETE));
        log.fine("POST " + workflowId + " - browser compatibility workflow DELETE");
        if (method != null && method.equals("delete")) {
            return deleteWorkflow();
        } else {
            return Response.serverError().build();
        }
    }

    // Browser compatibility PUT
    @POST
    @Consumes("application/json")
    public Response overwriteWorkflow(@QueryParam("method") String method, JSONObject request) throws Exception {
        AuthzManager.getInstance(ctx).authorize(
                new AuthzContext(securityCtx, AuthzContext.WORKFLOW_RESOURCE, workflowId, HttpMethod.PUT));
        log.fine("POST " + workflowId + " - browser compatibility workflow PUT");
        if (method != null && method.equals("put")) {
            return overwriteWorkflow(request);
        } else {
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes("application/json")
    public Response overwriteWorkflow(JSONObject request) throws Exception {
        AuthzManager.getInstance(ctx).authorize(
                new AuthzContext(securityCtx, AuthzContext.WORKFLOW_RESOURCE, workflowId, HttpMethod.PUT));
        log.fine("PUT " + workflowId + " (JSON) - overwrite workflow");
        log.finer("PUT " + workflowId + " (JSON) request: " + request);
        WorkflowManager.getInstance(ctx).updateWorkflow(workflowId, request.toString());
        return Response.ok().build();
    }

    @DELETE
    public Response deleteWorkflow() {
        AuthzManager.getInstance(ctx).authorize(
                new AuthzContext(securityCtx, AuthzContext.WORKFLOW_RESOURCE, workflowId, HttpMethod.DELETE));
        log.fine("DELETE " + workflowId + " - delete workflow");
        WorkflowManager.getInstance(ctx).removeWorkflow(workflowId);
        return Response.ok().build();
    }

    private static final Logger log = Logger.getLogger(WorkflowResource.class.getName());

}
